package com.JustTest;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.SystemDefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * Created by mike on 18/1/3.
 */
public class HttpTest {
    public static void main(String[] args) throws Exception {
        String[] citys = {"guangzhou"};
        for(String city:citys) {
//            String city = "hefei";
            Integer count = 0;
            String url = "https://" + city + ".anjuke.com/community";
            String cookies = "aQQ_ajkguid=ECB2F726-7495-DB35-37B4-6A775FE60228; expires=Sun, 10-Mar-2019 08:07:25 GMT; Max-Age=31622400; path=/; domain=.anjuke.com";
            String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36";


            Document doc = getResult(url, cookies, userAgent);


            HSSFWorkbook workbook = null;
            workbook = new HSSFWorkbook();
            //获取参数个数作为excel列数
            int columeCount = 6;
            //获取List size作为excel行数
            int rowCount = 20;
            HSSFSheet sheet = workbook.createSheet("sheet name");


            Elements districtElements = doc.getElementsByClass("items").get(0).getElementsByClass("pp-mod").get(0).getElementsByTag("a");

            for (int i = 1; i < districtElements.size(); i++) {
                String districtUrl = districtElements.get(i).attr("href");
                if(districtElements.get(i).html().indexOf("周边")!=-1){
                    break;
                }
                Document docDistrict = getResult(districtUrl, cookies, userAgent);

                Elements businessElements = docDistrict.getElementsByClass("sub-items").get(0).getElementsByTag("a");
                if (businessElements.size() <= 0) {
                    continue;
                }

                for (int j = 1; j < businessElements.size(); j++) {
                    Integer page = 1;


                    String businessUrl = businessElements.get(j).attr("href");
                    System.out.println("businessUrl = [" + businessUrl + "]");
                    while (true) {
                        Document docCommunity = getResult(businessUrl, cookies, userAgent, page);
                        Elements elements = docCommunity.getElementsByClass("li-info");
                        for (int k = 0; k < elements.size(); k++) {
                            String communityName = elements.get(k).getElementsByTag("h3").get(0).getElementsByTag("a").html();
                            String businessName = "";
                            try {
                                businessName = elements.get(k).getElementsByTag("address").get(0).html().split("］")[0].split("-")[1];
                            } catch (Exception e) {
                                System.out.println("糟了！没有值！！：" + elements.get(k).getElementsByTag("address"));
                            }
                            String distircitName = "";
                            try {
                                 distircitName = elements.get(k).getElementsByTag("address").get(0).html().split("］")[0].split("-")[0].split("［")[1] + "区";
                                 if(distircitName.equals("广州周边区")){
                                    break;
                                 }
                            }catch (Exception e){

                            }
                            String address = "";
                            try {
                                address = elements.get(k).getElementsByTag("address").get(0).html().split("］")[1];
                            } catch (Exception e) {
                                System.out.println("糟了！没有值！！：" + elements.get(k).getElementsByTag("address").get(0));
                            }

                            String xy = elements.get(k).getElementsByClass("bot-tag").get(0).getElementsByTag("a").get(1).attr("href");
                            String l1 = xy.split("l1=")[1].split("&l2=")[0];
                            String l2 = xy.split("l1=")[1].split("&l2=")[1].split("&l3")[0];
                            System.out.println("communityName:" + communityName + "districtName" + distircitName + " businessName:" + businessName + " address:" + address + " l1:" + l1 + " l2:" + l2);

                            HSSFRow row = sheet.createRow(count);
                            for (int n = 0; n <= columeCount - 1; n++) {
                                row.createCell(n);
                            }
                            row.getCell(0).setCellValue(communityName);
                            row.getCell(1).setCellValue(distircitName);
                            row.getCell(2).setCellValue(businessName);
                            row.getCell(3).setCellValue(address);
                            row.getCell(4).setCellValue(l2);
                            row.getCell(5).setCellValue(l1);

                            count++;


                        }
                        page++;

                        System.out.println("===========================================================================");

                        if (elements.size() <= 0) {
                            System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                            break;
                        }
                        TimeUnit.SECONDS.sleep(2);
                    }


                }


            }

            //写到磁盘上
            try {
                File file = new File("/Users/mike/GIT/city/" + city + ".xls");
                if (!file.exists()) {
                    file.createNewFile();
                    System.out.println("filePath is:" + file.getAbsolutePath());
                }
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                workbook.write(fileOutputStream);
                fileOutputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            System.out.println("cout is :" + count);
        }

    }

    private static Document getResult(String url, String cookies, String userAgent) throws IOException {
        HttpClient httpClient = new SystemDefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("Cookies", cookies);

        httpGet.setHeader("user-agent", userAgent);
        HttpResponse response = httpClient.execute(httpGet);
        String charset = "utf-8";
        HttpEntity resEntity = response.getEntity();
        Document doc = Jsoup.parse(EntityUtils.toString(resEntity, charset));
        return doc;
    }

    private static Document getResult(String url, String cookies, String userAgent, Integer page) throws IOException {
        HttpClient httpClient = new SystemDefaultHttpClient();
        HttpGet httpGet = new HttpGet(url + "p" + page);
        httpGet.setHeader("Cookies", cookies);

        httpGet.setHeader("user-agent", userAgent);
        HttpResponse response = httpClient.execute(httpGet);
        String charset = "utf-8";
        HttpEntity resEntity = response.getEntity();
        Document doc = Jsoup.parse(EntityUtils.toString(resEntity, charset));
        return doc;
    }

    public static void writeXlsx(String fileName, String communityName, String businessName, String address, String l2, String l1) throws Exception {

        HSSFWorkbook workbook = null;
        workbook = new HSSFWorkbook();
        //获取参数个数作为excel列数
        int columeCount = 5;
        //获取List size作为excel行数
        int rowCount = 20;
        HSSFSheet sheet = workbook.createSheet("sheet name");

        int rowNum = sheet.getLastRowNum();

        HSSFRow row = sheet.createRow(rowNum + 1);
        for (int n = 0; n <= columeCount - 1; n++) {
            row.createCell(n);
        }
        row.getCell(0).setCellValue(communityName);
        row.getCell(1).setCellValue(businessName);
        row.getCell(2).setCellValue(address);
        row.getCell(3).setCellValue(l2);
        row.getCell(4).setCellValue(l1);

        //写到磁盘上
        try {
            File file = new File("/Users/mike/GIT/" + fileName + ".xlsx");
            if (!file.exists()) {
                file.createNewFile();
                System.out.println("filePath is:" + file.getAbsolutePath());
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            workbook.write(fileOutputStream);
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
